package com.cis467.mrball;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MrBall extends Activity {

	private static final int MENU_RESUME = 0;

	private static final int MENU_RESTART = 1;

	private static final int MENU_TARE = 2;
	
	private static final int MENU_QUIT = 3;
	    
	/** A handle to the View in which the game is running. */
    private MrBallView mMrBallView;
    
    
    /**
     * Invoked during init to give the Activity a chance to set up its Menu.
     * 
     * @param menu the Menu to which entries may be added
     * @return true
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0, MENU_RESUME, 0, R.string.menu_resume);
        menu.add(0, MENU_RESTART, 0, R.string.menu_restart);
        menu.add(0, MENU_TARE, 0, R.string.menu_tare);
        menu.add(0, MENU_QUIT, 0, R.string.menu_quit);

        return true;
    }

    /**
     * Invoked when the user selects an item from the Menu.
     * 
     * @param item the Menu entry which was selected
     * @return true if the Menu item was legit (and we consumed it), false
     *         otherwise
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_RESUME:
            	mMrBallView.getThread().unpause();
                return true;
            case MENU_RESTART:
            	mMrBallView.getThread().startGame();
                return true;
            case MENU_TARE:
            	mMrBallView.getThread().tare();
            	return true;
            case MENU_QUIT:
            	finish();
                return true;
        }

        return false;
    }
    
    /**
     * Invoked when the Activity is created.
     * 
     * @param savedInstanceState a Bundle containing state saved from a previous
     *        execution, or null if this is a new execution
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // tell system to use the layout defined in our XML file
        setContentView(R.layout.mrball_layout);
        
        // get handles to view, thread, and sensor
        mMrBallView = (MrBallView) findViewById(R.id.mrball);
        
        // give the View a handle to the TextView used for messages
        mMrBallView.setTextView((TextView) findViewById(R.id.text));
        
        // give the view a handle to the saved instance state
        // this may be null, but it will be checked later
        mMrBallView.setSavedInstanceState(savedInstanceState);
    }

    /**
     * Notification that something is about to happen, to give the Activity a
     * chance to save state.
     * 
     * @param outState a Bundle into which this Activity should save its state
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // just have the View's thread save its state into our Bundle
        super.onSaveInstanceState(outState);
        mMrBallView.getThread().saveState(outState);
    }
}